/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.information.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.information.domain.ReserveConsult;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.information.service.ReserveConsultService;
import me.zhengjie.modules.information.domain.vo.ReserveConsultQueryCriteria;
import me.zhengjie.modules.information.mapper.ReserveConsultMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

import static me.zhengjie.modules.biz.domain.enums.ErrorCode.BODY_NOTNULL;
import static me.zhengjie.modules.biz.domain.enums.ErrorCode.USER_ID_NOT_NULL;
import static me.zhengjie.modules.pay.enums.StatusEnum.RESERVE_SOLVE;

/**
 * @description 服务实现
 * @author yiwei.he
 * @date 2024-05-09
 **/
@Service
@RequiredArgsConstructor
public class ReserveConsultServiceImpl extends ServiceImpl<ReserveConsultMapper, ReserveConsult> implements ReserveConsultService {

    private final ReserveConsultMapper reserveConsultMapper;

    @Override
    public PageResult<ReserveConsult> queryAll(ReserveConsultQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(reserveConsultMapper.findAll(criteria, page));
    }

    @Override
    public List<ReserveConsult> queryAll(ReserveConsultQueryCriteria criteria){
        return reserveConsultMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean create(ReserveConsult resources) {
        return save(resources);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ReserveConsult resources) {
        ReserveConsult reserveConsult = getById(resources.getId());
        reserveConsult.copy(resources);
        saveOrUpdate(reserveConsult);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<ReserveConsult> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ReserveConsult reserveConsult : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("用户id", reserveConsult.getUserId());
            map.put("用户名", reserveConsult.getName());
            map.put("手机号", reserveConsult.getPhone());
            map.put("咨询类型【0：其他 1：公招信息  2：培训考证  3：升学择校 4：招聘信息 5.高考志愿 6.专升本 7.单招 8.成人高考 9.新高考】", reserveConsult.getType());
            map.put("咨询状态【0：未处理 1：已处理】", reserveConsult.getStatus());
            map.put("咨询内容", reserveConsult.getRemark());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public PageResult<ReserveConsult> consultList(ReserveConsultQueryCriteria criteria, Page<Object> page) throws BizException {
        if(criteria.getUserId() == null){
            throw new BizException(USER_ID_NOT_NULL.getCode(),USER_ID_NOT_NULL.getMessage());
        }
        return PageUtil.toPage(reserveConsultMapper.findAll(criteria, page));
    }

    @Override
    @Transactional
    public Boolean editStatus(ReserveConsultQueryCriteria criteria) throws BizException {
        if(criteria.getId() == null){
            throw new BizException(BODY_NOTNULL.getCode(),BODY_NOTNULL.getMessage());
        }
        return this.update(new UpdateWrapper<ReserveConsult>().eq("id", criteria.getId()).set("status", RESERVE_SOLVE.getCode()));
    }

}